package com.music.mapper;

import com.music.pojo.Album;
import com.music.pojo.AlbumExample;

import java.util.AbstractCollection;
import java.util.List;

import com.music.pojo.Playlist;
import com.music.pojo.SingleSong;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.web.bind.annotation.ResponseBody;

public interface AlbumMapper {
    @SelectProvider(type=AlbumSqlProvider.class, method="countByExample")
    long countByExample(AlbumExample example);

    @DeleteProvider(type=AlbumSqlProvider.class, method="deleteByExample")
    int deleteByExample(AlbumExample example);

    @Delete({
        "delete from album",
        "where alid = #{alid,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer alid);

    @Insert({
        "insert into album (alid, aname, ",
        "sid, desc, publish_time, ",
        "pic)",
        "values (#{alid,jdbcType=INTEGER}, #{aname,jdbcType=INTEGER}, ",
        "#{sid,jdbcType=INTEGER}, #{desc,jdbcType=VARCHAR}, #{publishTime,jdbcType=DATE}, ",
        "#{pic,jdbcType=VARCHAR})"
    })
    int insert(Album row);

    @InsertProvider(type=AlbumSqlProvider.class, method="insertSelective")
    int insertSelective(Album row);

    @SelectProvider(type=AlbumSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="alid", property="alid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="aname", property="aname", jdbcType=JdbcType.INTEGER),
        @Result(column="sid", property="sid", jdbcType=JdbcType.INTEGER),
        @Result(column="desc", property="desc", jdbcType=JdbcType.VARCHAR),
        @Result(column="publish_time", property="publishTime", jdbcType=JdbcType.DATE),
        @Result(column="pic", property="pic", jdbcType=JdbcType.VARCHAR)
    })
    List<Album> selectByExample(AlbumExample example);

    @Select({
        "select",
        "alid, aname, sid, desc, publish_time, pic",
        "from album",
        "where alid = #{alid,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="alid", property="alid", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="aname", property="aname", jdbcType=JdbcType.INTEGER),
        @Result(column="sid", property="sid", jdbcType=JdbcType.INTEGER),
        @Result(column="desc", property="desc", jdbcType=JdbcType.VARCHAR),
        @Result(column="publish_time", property="publishTime", jdbcType=JdbcType.DATE),
        @Result(column="pic", property="pic", jdbcType=JdbcType.VARCHAR)
    })
    Album selectByPrimaryKey(Integer alid);

    @UpdateProvider(type=AlbumSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("row") Album row, @Param("example") AlbumExample example);

    @UpdateProvider(type=AlbumSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("row") Album row, @Param("example") AlbumExample example);

    @UpdateProvider(type=AlbumSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Album row);

    @Update({
        "update album",
        "set aname = #{aname,jdbcType=INTEGER},",
          "sid = #{sid,jdbcType=INTEGER},",
          "desc = #{desc,jdbcType=VARCHAR},",
          "publish_time = #{publishTime,jdbcType=DATE},",
          "pic = #{pic,jdbcType=VARCHAR}",
        "where alid = #{alid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Album row);

    /*
    * 查询专辑名绝对符合输入
    * 或歌手名包含输入的专辑
    * */

    @Select("select * from album where aname = #{param} or sid in (select sid from singer where sname = #{param}) limit 5")
    @ResultMap("fsaMap")
    List<Album> queryByAlbumNameOrSingerName(String param);

    @Select("select * from album where alid = #{alid}")
    @Results(id = "fsaMap",value = {
            @Result(id = true,column = "alid",property = "alid"),
            @Result(column = "aname",property = "aname"),
            @Result(column = "desc",property = "desc"),
            @Result(column = "publish_time",property = "publishTime"),
            @Result(column = "pic",property = "pic"),
            @Result(column = "sid",property = "singer",
            one = @One(select = "com.music.mapper.SingerMapper.findBySid")),
            @Result(column = "alid",property = "songs",
            many = @Many(select = "com.music.mapper.SingleSongMapper.findSongsByAlid")),
            @Result(column = "alid",property = "categorys",
            many = @Many(select = "com.music.mapper.CategoryMapper.findByAlid"))
    })
    Album findWithSongsAndSinger(Integer alid);

    @Select("select * from album where sid=#{sid}")
    List<Album> findBySid(int sid);

    @Select("select * from album where alid = #{alid}")
    Album findByAlid(int alid);

    @Select("select * from album where aname = #{search} or sid in (select sid from singer where sname = #{search})")
    @ResultMap("fsaMap")
    List<Album> findAlbumBySearch(String search);

    @Select("select * from album where sid=#{sid}")
    @Results(value = {
            @Result(id = true,column = "alid",property = "alid"),
            @Result(column = "aname",property = "aname"),
            @Result(column = "desc",property = "desc"),
            @Result(column = "publish_time",property = "publishTime"),
            @Result(column = "pic",property = "pic"),
            @Result(column = "alid",property = "songs",
                    many = @Many(select = "com.music.mapper.SingleSongMapper.findSongsByAlid")),
            @Result(column = "alid",property = "categorys",
                    many = @Many(select = "com.music.mapper.CategoryMapper.findByAlid"))
    })
    List<Album> findWithSongsBySid(int sid);


    @Select("select count(*) from user_favorite_album where alid=#{alid} and uid = #{uid}")
    Integer isFavorite(@Param("alid") Integer alid,@Param("uid") Integer uid);
    
    
    @Insert("insert into user_favorite_album (uid, alid) values (#{uid},#{alid});")
    void addFavoriteAlbum(@Param("alid") Integer alid,@Param("uid") Integer uid);

    @Delete("delete from user_favorite_album where alid = #{alid} and uid = #{uid}")
    void removeFavoriteAlbum(@Param("alid") Integer alid,@Param("uid") Integer uid);

    @Select("select count(*) from user_favorite_album where alid = #{alid}")
    Integer findCollectCount(Integer alid);

    @Select("select * from album where alid in (select alid from music where mid = #{mid})")
    Album findByMid(Integer mid);

    @Select("select * from album where alid in (select alid from user_favorite_album where uid = #{uid})")
    @ResultMap("fsaMap")
    List<Album> findFavoriteByUid(Integer uid);

    @Select("select * from album where sid in (select sid from user_follow_singer where uid = #{uid}) and album.publish_time>DATE_SUB(CURDATE(), INTERVAL 3 MONTH) order by publish_time desc")
    @ResultMap("fsaMap")
    List<Album> findUserFollowSingerAlbum(Integer uid);
}